//
//  RHReportHeaderView.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/11/15.
//

#import "RHReportHeaderView.h"
@interface RHReportHeaderView()

@end
@implementation RHReportHeaderView

-(instancetype)init
{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    kWeakSelf;
    self.backgroundColor = CFTabViewBgColor;
 
    UIView *bgView =  [[UIView alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH-30,105)];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView rounded:8 width:1 color:kBorderColor];
    
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(15);
        make.bottom.mas_offset(-15);
    }];
    
    __block UIView *lastView;
    //根据要显示的信息创建label
    NSArray* titles = @[@"设备ID".rh_localized,@"数据日期".rh_localized];
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
       
        UIView *itemView = [self itemViewWithTitle:title index:idx];
        [bgView addSubview:itemView];
        if (lastView) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
                make.top.equalTo(lastView.mas_bottom);
                make.height.equalTo(lastView);
            }];
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
                make.top.mas_offset(5);
                make.height.offset(50);
            }];
        }
        lastView = itemView;
        //线条
        if (idx != titles.count-1) {
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
            [itemView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.right.mas_offset(-15);
                make.height.offset(1);
                make.bottom.mas_offset(0);
            }];
        }
    }];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-5);
    }];
    
    self.frame = CGRectMake(0, 0, kUI_WINDOW_W, 45*titles.count+16);
}


-(UIView*)itemViewWithTitle:(NSString *)title index:(NSInteger)index
{
    kWeakSelf;
    
    
    UIView *itemView = kView;
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    [itemView addSubview:titleLab];
    titleLab.text = [NSString stringWithFormat:@"%@:",title.rh_localized];
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    titleLab.font = kSYSTEMFONT(14);
    titleLab.tag = 100+index;
    titleLab.textAlignment = NSTextAlignmentLeft;
    [titleLab sizeToFit];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.width.offset(titleLab.width+1);
    }];
    
    UIImageView *arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [itemView addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
        make.width.height.offset(18);
    }];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    [itemView addSubview:contentLab];
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    contentLab.font = kSYSTEMFONT(14);
    contentLab.tag = 200+index;
    contentLab.textAlignment = NSTextAlignmentRight;
    contentLab.numberOfLines = 2;
    contentLab.adjustsFontSizeToFitWidth = YES;
    contentLab.minimumScaleFactor = 0.8;
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_right).offset(5);
        make.right.equalTo(arrow.mas_left).offset(-3);
        make.centerY.mas_offset(0);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        if (weakSelf.clickBlock) {
            weakSelf.clickBlock(index);
        }
    }];
    [itemView addGestureRecognizer:tap];
    
    
    if (index == 0) {
        self.devLab = contentLab;
    }else if(index == 1){
        self.dateLab = contentLab;
    }
    
    return itemView;
}
@end
